package com.xique.door.controller;

import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.Elevator;
import com.xique.door.biz.service.IElevatorBizService;
import com.xique.door.constant.ElevatorConstant;
import com.xique.door.service.IElevatorService;
import com.xique.door.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2022/3/28 20:04
 */
@RestController
@RequestMapping("/elevator/task")
public class ElevatorTaskController extends BaseController {

    @Autowired
    private IElevatorBizService elevatorBizService;

    @Autowired
    private IElevatorService elevatorService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/syncEvent")
    public AjaxResult syncEvent() {
        Object token = redisUtil.get(ElevatorConstant.IotSessionTokenRedisKey);
        if (null == token) {
            elevatorBizService.getSessionToken();
        }
        List<Elevator> elevatorList = elevatorService.selectElevatorTaskList(new Elevator());
        elevatorBizService.syncEvents(elevatorList);
        return AjaxResult.success();
    }

    @PostMapping("/syncRun")
    public AjaxResult syncRun() {
        Object token = redisUtil.get(ElevatorConstant.IotSessionTokenRedisKey);
        if (null == token) {
            elevatorBizService.getSessionToken();
        }
        List<Elevator> elevatorList = elevatorService.selectElevatorTaskList(new Elevator());
        elevatorBizService.syncRuns(elevatorList);
        return AjaxResult.success();
    }

    @PostMapping("/syncReport")
    public AjaxResult syncReport() {
        Object token = redisUtil.get(ElevatorConstant.IotSessionTokenRedisKey);
        if (null == token) {
            elevatorBizService.getSessionToken();
        }
        List<Elevator> elevatorList = elevatorService.selectElevatorTaskList(new Elevator());
        elevatorBizService.syncReports(elevatorList);
        return AjaxResult.success();
    }
}
